package com.itcast.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.reggie.commen.BaseContext;
import com.itcast.reggie.pojo.R;
import com.itcast.reggie.pojo.ShoppingCart;
import com.itcast.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController{
    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加进购物车
    @PostMapping("/add")
    public R AddToCart(HttpSession session, @RequestBody ShoppingCart shoppingCart) {
        //给数据加上用户id值
        Long userId = (Long) session.getAttribute("UserId");
        shoppingCart.setUserId(userId);
        //获取一下套餐或者菜品的id
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        String dishFlavor = shoppingCart.getDishFlavor();
        //查一下该购物车数据是否存在
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq(dishId != null, "dish_id", dishId);
        wrapper.eq(setmealId != null, "setmeal_id", setmealId);
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        //如果该菜品不存在,添加入表
        if (cart == null) {
            shoppingCartService.save(shoppingCart);
            return R.success("成功");
        }
        //存在,数量加一
        Integer number = cart.getNumber();
        cart.setNumber(number + 1);
        shoppingCartService.updateById(cart);
        return R.success("成功");
    }

    //获取购物车信息
    @GetMapping("/list")
    public R GetAll() {

        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.gt("number", 0);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    //数量减少
    @PostMapping("/sub")
    public R reduce(@RequestBody ShoppingCart shoppingCart) {
        //使用正则表达式判断
        Long id = shoppingCart.getDishId() != null ? shoppingCart.getDishId() : shoppingCart.getSetmealId();
        String foodType = shoppingCart.getDishId() != null ? "dish_id" : "setmeal_id";
        //执行减少方法
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq(foodType, id);
        ShoppingCart onesShoppingCart = shoppingCartService.getOne(wrapper);
        Integer number = onesShoppingCart.getNumber();
        //删除数量为零的项
        if (number == 1) {
            shoppingCartService.removeById(onesShoppingCart.getId());
            return R.success("减少成功");
        }
        //执行减一操作
        onesShoppingCart.setNumber(number - 1);
        shoppingCartService.updateById(onesShoppingCart);
        return R.success("减少成功");
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        Long userId = (Long) session.getAttribute("UserId");
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        shoppingCartService.remove(wrapper);
        return R.success("清空成功");
    }

}
